<template>
  <div class="container">
    <!-- 步骤状态提示 -->
    <div class="step-alert">
      <div class="alert-box">
        <i
          class="el-alert__icon el-icon-success"
          style="font-size: 20px; color: #fff"
        ></i>
        <div style="font-size: 18px; color: #fff; margin-left: 10px">
          当前进程：{{ stepTitle }}
        </div>
      </div>
    </div>

    <div class="steps-container">
      <div style="width: 100%">
        <div
          style="display: flex; border: 2px solid #eedede; border-radius: 5px"
        >
          <div style="width: 50%; padding-left: 15px">
            <div style="font-size: 30px; margin-top: 20px; margin-bottom: 10px">
              扫码录入
            </div>
            <el-input
              style="border: 2px solid #409eff; font-size: 25px"
              type="textarea"
              rows="3"
              placeholder="请扫码"
              v-model="scanText"
              ref="myInput"
              clearable
              @keyup.enter.native="handleShowDialog"
            >
            </el-input>
          </div>
          <div class="verline"></div>
          <div style="width: 30%; margin-left: 20px">
            <div style="font-size: 30px; margin-top: 20px; margin-bottom: 10px">
              手动录入
            </div>
            <div style="display: flex; height: 50px; align-items: center">
              <div style="width: 60px">等级：</div>
              <el-input
                placeholder="请输入等级"
                v-model="userInputForm.grade"
                clearable
              >
              </el-input>
            </div>
            <div style="display: flex; height: 50px; align-items: center">
              <div style="width: 60px">批号：</div>
              <el-input
                placeholder="请输入批号"
                v-model="userInputForm.batch"
                clearable
              >
              </el-input>
            </div>

            <div style="text-align: center">
              <el-button type="primary" @click="handleNext">下一步</el-button>
            </div>
          </div>
        </div>
        <div style="font-size: 16px; margin: 40px 0 0 10px; width: 80%">
          <div style="display: flex; font-size: 16px; margin-bottom: 15px">
            <div>等级：{{ formInfo.grade }}</div>
            <div style="margin-left: 15px">批次：{{ formInfo.batch }}</div>
          </div>
          <div style="margin-bottom: 10px">
            <div style="margin-bottom: 5px">计算结果如下：</div>
            <div
              v-for="(item, index) in calResurtArr"
              :key="index"
              style="margin-top: 5px"
            >
              <div>{{ item }}</div>
            </div>
          </div>
          <div style="display: flex">
            <table border="1" cellpadding="8" style="border-collapse: collapse">
              <thead>
                <tr>
                  <th style="background: #f5f7fa; width: 120px">时间（秒）</th>
                  <th style="background: #f5f7fa; width: 200px">数据值</th>
                </tr>
              </thead>
              <tbody>
                <tr
                  v-for="(item, index) in [60, 90, 100, 120, 150, 240, 300]"
                  :key="index"
                >
                  <td style="text-align: center">{{ item }}</td>
                  <td style="text-align: center">{{ tempArr[index] }}</td>
                </tr>
              </tbody>
            </table>
            <div
              style="
                font-size: 30px;
                margin-left: 200px;
                margin-top: 80px;
                color: #13ce66;
              "
            >
              <div>比重：{{ dialogForm.sg }}</div>
              <div style="margin-top: 20px">重量：{{ dialogForm.weight }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogShow"
      width="600px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        ref="formRef"
        label-width="60px"
        :model="dialogForm"
        :rules="rules"
      >
        <el-form-item label="等级" prop="grade">
          <el-input
            v-model="dialogForm.grade"
            placeholder="请输入"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="比重" prop="sg">
          <el-input
            v-model="dialogForm.sg"
            placeholder="请输入"
            clearable
            type="number"
            size="small"
            @input="handleSgChange"
          />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input
            type="textarea"
            v-model="dialogForm.weight"
            clearable
            readonly
            size="small"
            rows="2"
            style="font-size: 25px"
          />
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <!-- 如果数据库查到比重数据，就不显示保存按钮 -->
        <el-button type="primary" @click="handleSubmit" v-if="haveNoSg"
          >保存比重</el-button
        >
        <!-- <el-button type="primary" @click="handleGoOn">继 续</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  saveGradeWeightSg,
  rpaCeding1Click,
  rpaJiansuoClick,
  cedingyilaidenglu,
  getSgByGrade,
  getQakansaByBatch,
  biaoshiExport,
  findClosestData,
  getInspData,
  saveINSPData,
  savePrdQaPlanresult,
  isExperimentComplete,
  findExcelData,
  saveExcelData,
} from "@/api/weight";
import { errMsg, succMsg } from "../../utils/message";
import hasDirective from "/src/directive/permission/btnPermiss.js";
import { Loading } from "element-ui";

import { MessageBox, Message } from "element-ui";
var loadingInstance = null;
export default {
  directives: {
    has: hasDirective,
  },
  data() {
    return {
      dialogLoading: false,
      startLoading: false,
      calcLoading: false,
      scanText: "",
      grade: "",
      batch: "",
      // 记录等级、批号
      formInfo: {},
      dialogShow: false,
      dialogForm: {
        grade: "",
        sg: "",
        weight: "",
      },
      haveNoSg: false,
      rules: {
        grade: [{ required: true, message: "请输入等级", trigger: "blur" }],
        sg: [{ required: true, message: "请输入系数", trigger: "blur" }],
        weight: [{ required: true, message: "请输入重量", trigger: "blur" }],
      },
      // 手动录入
      userInputForm: {
        grade: "",
        batch: "",
      },
      burnTimer: null, // 主定时器
      // positionTimer: null, // 嵌套定时器
      nextTimer: null,
      statrBtnText: "保存数据并分析",
      startDisabled: true,

      noArr: [
        "+98",
        "921",
        "TT8",
        "+99",
        "+95",
        "A61",
        "T33",
        "+92",
        "+97",
        "894",
        "893",
        "892",
        //"011",
      ],
      akansaArr: [], // 试验项number数组：+98、921、TT8、+99、+95、A61、T33、+92、+97、894、893、892、011.....
      cedingArr: [], // akansaArr过滤后，100、101各只保留一条
      ceding100Data: null,
      ceding101Data: null,
      temp60: "",
      temp90: "",
      temp100: "",
      temp120: "", //新增
      temp150: "", //新增
      temp240: "", //新增
      temp300: "",
      tempArr: [],
      currentNumber: "", //当前实验项编号是100还是101
      csvNumber: "0001", //保存CSV文件，0001还是0002
      isOver: false,
      intervalId: null,
      calResurtArr: [],
    };
  },
  created() {
    this.stepTitle = "输入等级计算重量";

    // G630AY-T32
    // this.getNumberByBatch("509AT89");
    // this.getNumberByBatch('509AT89C')

    // this.handleCedingClick();
    // this.testInsert()

    // this.monitorFireIsOver();
  },

  beforeUnmount() {
    if (this.intervalId) {
      clearInterval(this.intervalId);
    }
    this.cancelTimer();
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.myInput.focus();
    });
  },
  methods: {
    // 扫码拿数据
    handleShowDialog() {
      // 清除定时器
      this.cancelTimer();
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
      console.log("扫码结果>>>>>>>>>>>>>", this.scanText);
      if (!this.scanText.includes(";") && !this.scanText.includes("；")) {
        this.$message({
          message: "识别失败！",
          type: "error",
        });
        this.scanText = "";
        return;
      } else if (this.scanText.includes("；")) {
        this.$message({
          message: "请切换输入法为英文再扫码！！",
          type: "error",
          duration: 5 * 1000,
        });
        this.scanText = "";
        return;
      } else {
        let qas = this.scanText.split(";")[1];
        if (qas.includes("质量保证")) {
          this.$message({
            message: "请切换输入法为英文再扫码！",
            type: "error",
          });
          return;
        }
      }
      let grade = this.scanText.split(";")[2];
      // 空格换成-
      // if (grade.indexOf(" ") !== -1) {
      //   grade = grade.replace(" ", "-");
      // }
      let batch = this.scanText.split(";")[3];
      // if (batch.length > 7) {
      //   batch = batch.substring(0, 7);
      // }
      this.scanNext(grade, batch);
    },
    // 手动输入
    handleNext() {
      // 清除定时器
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
      this.cancelTimer();
      // -换成空格
      if (this.userInputForm.grade.indexOf("-") !== -1) {
        this.userInputForm.grade = this.userInputForm.grade.replace("-", " ");
      }
      if (this.userInputForm.batch.length == 7) {
        this.userInputForm.batch = this.userInputForm.batch + "C";
      }
      if (!this.userInputForm.grade || !this.userInputForm.batch) {
        this.$message({
          message: "请输入等级和编码！",
          type: "warning",
        });
      } else {
        this.scanNext(this.userInputForm.grade, this.userInputForm.batch);
      }
    },

    // 显示弹窗信息
    scanNext(grade, batch) {
      this.temp60 = "";
      this.temp90 = "";
      this.temp100 = "";
      this.temp120 = "";
      this.temp150 = "";
      this.temp240 = "";
      this.temp300 = "";
      this.tempArr = [];
      this.isOver = false;
      this.calResurtArr = [];

      this.grade = grade;
      this.batch = batch;
      let infoD = {
        grade: this.grade,
        batch: this.batch,
      };
      this.formInfo = { ...infoD };
      this.dialogForm.grade = "";
      this.dialogForm.weight = "";
      this.dialogForm.sg = "";
      // this.dialogShow = true;
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.resetFields();
        }
      });
      // sg比重维护
      let sgInfo = {
        ...this.formInfo,
      };
      let { grade: queryGrade } = sgInfo;
      if (queryGrade.indexOf(" ") !== -1) {
        queryGrade = queryGrade.replace(" ", "-");
      }
      if (queryGrade.indexOf("-") !== -1) {
        // 如果-后面一位是数字，取-前面的部分就可以，如果-后面一位是字母，grade取-前面的部分和-后面的第一位
        if (!/[0-9]/.test(queryGrade.split("-")[1].charAt(0))) {
          queryGrade =
            queryGrade.split("-")[0] + "-" + queryGrade.split("-")[1].charAt(0);
        }else{
            queryGrade = queryGrade.split("-")[0] 
        }
      }
      console.log("queryGrade>>>>>>>>>>>>", queryGrade);
      getSgByGrade({
        // grade: this.grade,
        grade: queryGrade,
      }).then((res) => {
        console.log("获取比重数据>>>>>>>>>>>>>", res);
        this.dialogForm.grade = this.grade;
        if (res.code == "00000") {
          if (res.data) {
            //查找到等级，输入比重自动计算称重重量；
            this.dialogForm.sg = res.data.sg;
            this.dialogForm.weight = (3.5 * parseFloat(res.data.sg)).toFixed(1);
            // this.dialogForm.weight = res.data.weight;
            this.haveNoSg = false;
            this.nextTimer = setTimeout(() => {
              // 继续下一步
              this.getBatchNextInput();
            }, 1 * 1000);
          } else {
            // 不存在，再去找grade的-前的
            // this.getSgByNewGrade();
             // 如果不存在则提示保存等级库，保存之后才能继续
            this.dialogShow = true;
            this.haveNoSg = true;
          }
        }
      });
    },

    // getSgByNewGrade() {
    //   let sgInfo = {
    //     ...this.formInfo,
    //   };
    //   let { grade } = sgInfo;
    //   if (grade.indexOf(" ") !== -1) {
    //     grade = grade.split(" ")[0];
    //   }
    //   console.log("getSgByNewGrade>>>>>>>>>>>>", grade);
    //   getSgByGrade({
    //     grade,
    //   }).then((res) => {
    //     console.log("获取比重数据2>>>>>>>>>>>>>", res);
    //     if (res.code == "00000") {
    //       if (res.data) {
    //         //查找到等级，输入比重自动计算称重重量；
    //         this.dialogForm.sg = res.data.sg;
    //         this.dialogForm.weight = (3.5 * parseFloat(res.data.sg)).toFixed(1);
    //         // this.dialogForm.weight = res.data.weight;
    //         this.haveNoSg = false;
    //         this.nextTimer = setTimeout(() => {
    //           // 继续下一步
    //           this.getBatchNextInput();
    //         }, 1 * 1000);
    //       } else {
    //         // 如果不存在则提示保存等级库，保存之后才能继续
    //         this.dialogShow = true;
    //         this.haveNoSg = true;
    //       }
    //     }
    //   });
    // },

    // 输入比重
    handleSgChange(val) {
      console.log("val>>>>>>>>>>", val);
      // if (this.dialogForm.sg) {
      //计算公式：称重重量=3.5gx比重 (保留一位小数)；
      // this.dialogForm.weight = (3.5 * val).toFixed(1);
      if (val) {
        this.dialogForm.weight = (3.5 * val).toFixed(1);
      } else {
        this.dialogForm.weight = "";
      }
      // }
    },

    // 等级、比重入库
    handleSubmit() {
      this.dialogLoading = true;
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 保存到数据库oracle
          let sgInfo = {
            ...this.dialogForm,
          };
          let { grade, sg } = sgInfo;
          if (grade.indexOf(" ") !== -1) {
            grade = grade.replace(" ", "-");
          }
          if (grade.indexOf("-") !== -1) {
            // 如果-后面一位是数字，取-前面的部分就可以，如果-后面一位是字母，grade取-前面的部分和-后面的第一位
            if (!/[0-9]/.test(grade.split("-")[1].charAt(0))) {
              grade =
                grade.split("-")[0] + "-" + grade.split("-")[1].charAt(0);
            }else{
                grade = grade.split("-")[0] 
            }
          }
          saveGradeWeightSg({
            grade,
            sg,
          }).then((res) => {
            if (res.code == "00000") {
              succMsg("保存成功!");
              this.dialogLoading = false;
              this.dialogShow = false;
            } else {
              this.$message({
                message: "保存失败！",
                type: "error",
              });
            }

            // this.stepTitle = "实验中";
            // this.statrBtnText = "点击保存数据并分析";
            // this.startDisabled = false;
            // this.getNumberByBatch(this.batch);
            this.getBatchNextInput();
          });
        }
      });
    },
    getBatchNextInput() {
      this.stepTitle = "实验中";
      this.statrBtnText = "点击保存数据并分析";
      this.startDisabled = false;
      this.getNumberByBatch(this.batch);
    },
    // 弹窗继续
    handleGoOn() {
      this.dialogShow = false;
      this.dialogLoading = false;

      // this.stepTitle = "实验中";
      // this.statrBtnText = "点击保存数据并分析";
      // this.startDisabled = false;
      // this.getNumberByBatch(this.batch);
    },

    // 去数据库获取实验编号101、100
    getNumberByBatch(batch) {
      // 获取当前批次对应的实验项，不止curelasto实验项,还有其他TMA实验之类的
      getQakansaByBatch({
        batchNo: batch,
      }).then((res) => {
        console.log("获取实验编号数据res>>>>>>>>>>>>>", res);
        this.dialogForm.grade = this.grade;
        this.ceding100Data = null;
        this.ceding101Data = null;
        this.cedingArr = [];
        if (res.code == "00000") {
          if (res.data && res.data.length > 0) {
            let numberArr = [];
            res.data.map((item) => {
              if (this.noArr.includes(item.inspitm_CD)) {
                // 再判断是100还是101
                if (["892", "893", "894"].includes(item.inspitm_CD)) {
                  numberArr.push({
                    number: "101",
                    grade: this.grade,
                    batch: this.batch,
                    inspitm_CD: item.inspitm_CD,
                    inspitm_NM: item.inspitm_NM,
                    insp_SEQ: item.insp_SEQ,
                    insp_KND_TYP: item.insp_KND_TYP,
                    quantitative_MAX: item.quantitative_MAX,
                    quantitative_MIN: item.quantitative_MIN,
                  });
                  this.ceding101Data = {
                    number: "101",
                    grade: this.grade,
                    batch: this.batch,
                  };
                } else {
                  numberArr.push({
                    number: "100",
                    grade: this.grade,
                    batch: this.batch,
                    inspitm_CD: item.inspitm_CD,
                    inspitm_NM: item.inspitm_NM,
                    insp_SEQ: item.insp_SEQ,
                    insp_KND_TYP: item.insp_KND_TYP,
                    quantitative_MAX: item.quantitative_MAX,
                    quantitative_MIN: item.quantitative_MIN,
                  });
                  this.ceding100Data = {
                    number: "100",
                    grade: this.grade,
                    batch: this.batch,
                  };
                }
              }
            });
            // this.akansaArr = res.data
            this.akansaArr = numberArr;
            // numberArr过滤,100、101都只保留一条
            const map = new Map();
            numberArr.forEach((item) => {
              if (!map.has(item.number)) {
                map.set(item.number, item);
              }
            });
            // cedingArr只包含100、101的实验，最多两条数据(100、101各一条)
            this.cedingArr = Array.from(map.values());
            console.log("cedingArr>>>>>>>>>>>>>", this.cedingArr);
            if (this.cedingArr.length > 0) {
              // 有高温实验，未做, 需要提示
              if (this.ceding101Data) {
                // 判断高温实验有没有做
                getInspData({
                  grade: this.grade,
                  batch: this.batch,
                }).then((res) => {
                  console.log(
                    "批次:" + this.batch + "实验数据>>>>>>>>>>>",
                    res
                  );
                  if (res.data) {
                    let inspData = [];
                    if (res.data.length > 0) {
                      inspData = res.data;
                    }
                    let inspIds = inspData.map((item) => {
                      return item.insp;
                    });
                    console.log("inspIds>>>>>>>>>>", inspIds);

                    if (!inspIds.includes("101")) {
                      // 有高温实验，未做,这边加提示
                      this.$confirm("有高温实验，是否继续?", "提示", {
                        confirmButtonText: "继续",
                        cancelButtonText: "取消",
                        type: "warning",
                        distinguishCancelAndClose: true, //取消和x区分
                      })
                        .then(() => {
                          this.handleCedingClick();
                        })
                        .catch(() => {
                          // 取消
                          this.$refs.myInput.focus();
                          this.scanText = "";
                        });
                    } else {
                      // 有高温实验，已做
                      this.handleCedingClick();
                    }
                  }
                });
              } else {
                this.handleCedingClick();
              }
            } else {
              this.$alert("未找到实验项", "提示", {
                confirmButtonText: "确定",
                callback: (action) => {
                  this.scanText = "";
                  this.$refs.myInput.focus();
                },
              });
            }
          } else {
            this.$message({
              message: "未获取到实验编号！",
              type: "error",
            });
          }
        }
      });
    },

    cancelTimer() {
      if (this.burnTimer) {
        clearTimeout(this.burnTimer);
        this.burnTimer = null;
      }
      if (this.nextTimer) {
        clearTimeout(this.nextTimer);
        this.nextTimer = null;
      }
      //   if (this.positionTimer) {
      //     clearTimeout(this.positionTimer);
      //     this.positionTimer = null;
      //   }
    },

    // 点击测试,输入100/101、等级、批号、测定次数
    // 可能会输入两条数据：高温101、常温100两条数据
    // rpaCeding1Click输入一条数据，rpaCeding2Click输入两条数据(100、101)
    // 最后又改为只输入一条数据了
    handleCedingClick() {
      console.log("ceding100Data>>>>>", this.ceding100Data);
      console.log("ceding101Data>>>>>", this.ceding101Data);

      // 查找对应批次的已做实验数据
      let inspData = [];
      getInspData({
        grade: this.grade,
        batch: this.batch,
      }).then((res) => {
        console.log("批次:" + this.batch + "实验数据>>>>>>>>>>>", res);
        if (res.data) {
          if (res.data.length > 0) {
            inspData = res.data;
          }
          let inspIds = inspData.map((item) => {
            return item.insp;
          });
          console.log("getInspData--inspIds>>>>>>>>>>", inspIds);

          // 需要做100实验
          if (this.ceding100Data) {
            // 已做100实验
            if (inspIds.includes("100")) {
              // 需要做101实验
              if (this.ceding101Data) {
                // 已做101实验
                if (inspIds.includes("101")) {
                  this.$alert("该批实验已做完", "提示", {
                    confirmButtonText: "确定",
                    callback: (action) => {
                      this.scanText = "";
                    },
                  });
                  this.$refs.myInput.focus();
                  this.startDisabled = true;
                } else {
                  // 未做101实验，去做101
                  this.currentNumber = "101";
                  //第二次实验，所以CSV生成的是0002
                  this.csvNumber = "0002";
                  console.log(
                    "已做100实验，未做101实验，去做101>>>>>>>>>>>>>>>"
                  );
                  this.startDisabled = false;
                  rpaCeding1Click({
                    num: "101",
                    grade: this.grade,
                    batch: this.batch,
                  }).then((res) => {
                    console.log("第二次实验，对应101res>>>>>>>>>>>", res);
                    this.monitorFireIsOver();
                  });
                }
              } else {
                // 不需要101实验
                this.$alert("该批实验已做完", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.scanText = "";
                  },
                });
                this.$refs.myInput.focus();
                this.startDisabled = true;
              }
            } else {
              // 未做100实验
              this.currentNumber = "100";
              // 有101实验
              if (this.ceding101Data) {
                // 已做101实验
                if (inspIds.includes("101")) {
                  //已做101实验，未做100实验，准备去做100实验，所以是第二次实验，CSV生成的是0002
                  console.log(
                    "已做101实验，未做100实验，准备去做100实验，所以是第二次实验，CSV生成的是0002"
                  );
                  this.csvNumber = "0002";
                } else {
                  // 未做101实验
                  //第一次实验，所以CSV生成的是0001
                  console.log("第一次实验，所以CSV生成的是0001");
                  this.csvNumber = "0001";
                }
              } else {
                // 没有101实验
                // 第一次实验，所以CSV生成的是0001
                console.log("第一次实验，所以CSV生成的是0001");
                this.csvNumber = "0001";
              }
              this.startDisabled = false;
              // 去做100实验
              rpaCeding1Click({
                num: "100",
                grade: this.grade,
                batch: this.batch,
              }).then((res) => {
                console.log("csvNumber==" + this.csvNumber + "---100实验", res);
                this.monitorFireIsOver();
              });
            }
          } else {
            // 不需要做100实验
            // 需要做101实验
            if (this.ceding101Data) {
              // 已做101实验
              if (inspIds.includes("101")) {
              } else {
                // 未做101实验，去做101
                this.currentNumber = "101";
                // 第一次实验，所以是0001
                this.csvNumber = "0001";
                console.log("第一次实验，所以是0001>>>>>>>>>>>>");
                this.startDisabled = false;
                rpaCeding1Click({
                  num: "101",
                  grade: this.grade,
                  batch: this.batch,
                }).then((res) => {
                  console.log("【第一次实验101res>>>>>>>>>>>", res);
                  this.monitorFireIsOver();
                });
              }
            }
          }
        }
      });
    },

    // 监听燃烧是否结束
    monitorFireIsOver() {
      // 5分钟后监听
      this.burnTimer = setTimeout(() => {
        // 10秒监听一次
        this.intervalId = setInterval(() => {
          this.ocrExperimentComplete();
        }, 10000);
      }, 290 * 1000);
      // }, 5 * 1000);
    },

    // 开始
    handleStart() {
      // if (!this.grade) {
      //   this.$message({
      //     message: "请先扫码！",
      //     type: "error",
      //   });
      //   return;
      // }
      this.stepTitle = "保存分析数据中...";
      this.statrBtnText = "分析中...";
      this.startDisabled = true;
      this.startLoading = true;
      // 燃烧定时器
      // this.burnTimer = setTimeout(() => {
      //燃烧实验结束后测定窗口会自动关掉，我需要手动关闭[测定依赖登录]窗口
      cedingyilaidenglu().then((res) => {
        this.startLoading = false;
        // 检索保存，需要先关闭确定保存数据小弹窗
        rpaJiansuoClick().then((res) => {
          console.log("检索保存>>>>>>>>>>>", res);
          // 表示里面导出数据，如果有重名，需要先删掉
          biaoshiExport({
            grade: this.grade + "0001",
          }).then((res) => {
            // 分析数据
            this.findTempData();
            // 分析excel
            this.excelAnalisys();
          });
        });
      });
      // }, 2 * 1000);
    },

    findTempData() {
      findClosestData({
        grade: this.grade + "0001",
        // grade: this.grade + this.csvNumber,
      }).then((res) => {
        console.log("温度数据>>>>>>>", res);
        this.tempArr = [];
        if (res.code == "00000") {
          let erroMsg = "";
          res.data.map((item, index) => {
            if (item == "读取文件时发生错误") {
              erroMsg = "读取文件时发生错误";
            } else if (item == "未找到") {
              erroMsg = "未找到";
            }
          });
          if (erroMsg) {
            console.log("读取文件错误：", erroMsg);
            this.statrBtnText = erroMsg;
            this.startDisabled = true;
            this.$message({
              message: erroMsg,
              type: "error",
            });
            return;
          }
          res.data.map((item, index) => {
            if (item && item.includes(",")) {
              let resTemp = item.split(",")[2];
              if (index == 0) {
                this.temp60 = resTemp;
              } else if (index == 1) {
                this.temp90 = resTemp;
              } else if (index == 2) {
                this.temp100 = resTemp;
              } else if (index == 3) {
                this.temp120 = resTemp;
              } else if (index == 4) {
                this.temp150 = resTemp;
              } else if (index == 5) {
                this.temp240 = resTemp;
              } else if (index == 6) {
                this.temp300 = resTemp;
              }
              this.tempArr.push(resTemp);
            }
          });
          console.log("tempArr>>>>>>>>>>>>>>>", this.tempArr);
          this.stepTitle = "数据分析已完成，请查看";
          this.statrBtnText = "数据分析已完成，请查看";
          this.scanText = "";
          // this.$refs.myInput.focus();

          // 1、保存数据到QA_RPA_CURE_INSP
          saveINSPData({
            insp: this.currentNumber,
            grade: this.formInfo.grade,
            batch: this.formInfo.batch,
          }).then((res) => {
            console.log("保存数据到QA_RPA_CURE_INSP>>>>>>>", res);
          });

          // 2、保存数据试验项
          console.log("this.akansaArr>>>>>>>>>>>>>", this.akansaArr);
          // 保存当前做的实验项
          this.akansaArr.forEach((item) => {
            if (item.number != this.currentNumber) {
              return;
            }
            let resultStr = "";
            // inspitm_CD实验id
            if (item.inspitm_CD == "+98") {
              // 150秒测定值/300秒测定值x100%  100
              resultStr = (
                (parseFloat(this.temp150) / parseFloat(this.temp300)) *
                100
              ).toFixed(1);
              let calResurt98 =
                "实验序号+98，常温实验，" +
                "150秒测定值/300秒测定值x100% = " +
                resultStr +
                "%";
              this.calResurtArr.push(calResurt98);
            } else if (item.inspitm_CD == "921") {
              // 90秒测定值  100
              resultStr = parseFloat(this.temp90).toFixed(1);
              let calResurt921 =
                "实验序号921，常温实验，" + "90秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt921);
            } else if (item.inspitm_CD == "TT8") {
              // Curelast 160'c 60sec 100
              resultStr = parseFloat(this.temp60).toFixed(1);
              let calResurtTT8 =
                "实验序号TT8，常温实验，" + "60秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurtTT8);
            } else if (item.inspitm_CD == "+99") {
              // 60秒测定值/300秒测定值x100% 100
              resultStr = (
                (parseFloat(this.temp60) / parseFloat(this.temp300)) *
                100
              ).toFixed(1);
              let calResurt99 =
                "实验序号+99，常温实验，" +
                "60秒测定值/300秒测定值x100% = " +
                resultStr +
                "%";
              this.calResurtArr.push(calResurt99);
            } else if (item.inspitm_CD == "+95") {
              // 100秒测定值/300秒测定值x100% 100
              resultStr = (
                (parseFloat(this.temp100) / parseFloat(this.temp300)) *
                100
              ).toFixed(1);
              let calResurt95 =
                "实验序号+95，常温实验，" +
                "100秒测定值/300秒测定值x100% = " +
                resultStr +
                "%";
              this.calResurtArr.push(calResurt95);
            } else if (item.inspitm_CD == "A61") {
              // 100秒测定值/300秒测定值x100%
              resultStr = (
                (parseFloat(this.temp100) / parseFloat(this.temp300)) *
                100
              ).toFixed(1);
              let calResurtA61 =
                "实验序号A61，常温实验，" +
                "100秒测定值/300秒测定值x100% = " +
                resultStr +
                "%";
              this.calResurtArr.push(calResurtA61);
            } else if (item.inspitm_CD == "T33") {
              // 120秒测定值/240秒测定值x100%
              resultStr = (
                (parseFloat(this.temp120) / parseFloat(this.temp240)) *
                100
              ).toFixed(1);
              let calResurtT33 =
                "实验序号T33，常温实验，" +
                "120秒测定值/240秒测定值x100% = " +
                resultStr +
                "%";
              this.calResurtArr.push(calResurtT33);
            } else if (item.inspitm_CD == "+92") {
              // 100秒测定值 100
              resultStr = parseFloat(this.temp100).toFixed(1);
              let calResurt92 =
                "实验序号+92，常温实验，" + "100秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt92);
            } else if (item.inspitm_CD == "+97") {
              // Curelasto 150s 100
              resultStr = parseFloat(this.temp150).toFixed(1);
              let calResurt97 =
                "实验序号+97，常温实验，" + "150秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt97);
            } else if (item.inspitm_CD == "894") {
              // 175'c 300sec 101
              resultStr = parseFloat(this.temp300).toFixed(1);
              let calResurt894 =
                "实验序号894，高温实验，" + "300秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt894);
            } else if (item.inspitm_CD == "893") {
              // 175'c 60sec 101
              resultStr = parseFloat(this.temp60).toFixed(1);
              let calResurt893 =
                "实验序号893，高温实验，" + "60秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt893);
            } else if (item.inspitm_CD == "892") {
              // Curelast175'C 90sec 101
              resultStr = parseFloat(this.temp90).toFixed(1);
              let calResurt892 =
                "实验序号892，高温实验，" + "90秒测定值 = " + resultStr;
              this.calResurtArr.push(calResurt892);
            }
            let parms = {
              bpr_itemid: item.inspitm_CD, //实验项id
              bpr_itemname: item.inspitm_NM,
              bpr_batchno: this.formInfo.batch,
              bpr_result: resultStr, //实验结果
              bpr_qa: "QARPA", //反馈人
              // bpr_qadate:new Date(), //反馈时间
              bpr_status: "1", // 子任务状态 0默认 1结束
              bpr_itmcd: this.formInfo.grade, //等级
              bpr_inspseq: item.insp_SEQ, //实验项顺序
              bpr_insptyp: item.insp_KND_TYP, //实验项类型，对应数据库查询字段
              quantitative_max: item.quantitative_MAX,
              quantitative_min: item.quantitative_MIN,
            };
            console.log("入库参数parms>>>>>>>>>>", parms);
            savePrdQaPlanresult(parms).then((res) => {
              console.log("保存数据到prd_bcd_qa_planresult>>>>>>>", res);
              succMsg("数据入库成功!");
            });
          });
        }
      });
    },

    // 测试插入数据
    testInsert() {
      let parms = {
        bpr_itemid: "A61", //实验项id
        bpr_itemname: null,
        bpr_batchno: "509AU29C",
        bpr_result: "20", //实验结果
        bpr_qa: "QARPA", //反馈人
        // bpr_qadate:new Date(), //反馈时间
        bpr_status: "1", // 子任务状态 0默认 1结束
        bpr_itmcd: "G630AY U30", //等级
        bpr_inspseq: "28", //实验项顺序
        bpr_insptyp: "2", //实验项类型，对应数据库查询字段
        quantitative_max: 87.1,
        quantitative_min: 23.4,
      };
      console.log("入库参数parms>>>>>>>>>>", parms);
      savePrdQaPlanresult(parms).then((res) => {
        console.log("保存数据到prd_bcd_qa_planresult>>>>>>>", res);
        succMsg("数据入库成功!");
      });
    },

    ocrExperimentComplete() {
      isExperimentComplete().then((res) => {
        console.log("监听燃烧是否结束>>>>>>", res.data);
        // #ta* #rar   测定中的：EEA YI+ VP
        if (res.data.includes("#ra") || res.data.includes("#ta")) {
          this.isOver = true;
          console.log("燃烧已结束。");
          if (this.intervalId) {
            clearInterval(this.intervalId);
          }
          this.cancelTimer();
          this.handleStart();
        } else {
          console.log("燃烧未结束");
        }
      });
    },
    excelAnalisys() {
      findExcelData({
        grade: this.grade + "0001",
      }).then((res) => {
        console.log("温度数据>>>>>>>", res);
        this.excelDataArr = [];
        if (res.code == "00000") {
          // if(res.data.length > 0 && (res.data[0] == "读取文件时发生错误" || res.data[0] == "未找到")){
          //   console.log("读取文件错误：");
          //   this.startDisabled = true;
          //   this.$message({
          //     message: "未找到或读取文件错误：",
          //     type: "error",
          //   });
          //   return;
          // }
          res.data.map((item, index) => {
            if (item && item.includes(",")) {
              let resTime = item.split(",")[0];
              let resvalue = item.split(",")[2];
              this.excelDataArr.push({
                PLAN_LOT: this.batch,
                GRADE: this.grade,
                NODE_TIME: resTime,
                NODE_VAL: resvalue,
                // node_type:"",
                // createdate:"",
              });
            }
          });
          console.log("excelDataAr>>>>>>>>>>>>", this.excelDataArr);
          saveExcelData(this.excelDataArr).then((res) => {
            console.log("保存数据到prd_bcd_qa_planresult>>>>>>>", res);
            succMsg("数据入库成功!");
          });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.container {
  height: 100%;
  padding: 20px;
}
.steps-container {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.step-content {
  margin-top: 30px;
  width: 70%;
  text-align: center;
  padding-top: 30px;
}

.line {
  height: 2px;
  width: 100%;
  background: #eedede;
}
.verline {
  margin-left: 30px;
  height: 210px;
  width: 2px;
  background: #eedede;
}

.grade-input .el-input__inner {
  height: 40px;
  border-color: #13ce66 !important;
}
.custom-button {
  height: 60px;
  line-height: 60px;
  padding: 0 40px;
  font-size: 30px;
  background-color: #13ce66;
  border-radius: 30px;
  color: #fff;
}

.custom-button:hover {
  color: #fff; /* 悬停颜色 */
  cursor: pointer;
  background-color: #13ce66;
  border-radius: 30px;
}

.custom-button.is-disabled,
.custom-button.is-disabled:hover,
.custom-button.is-disabled:focus {
  background: #999999;
}

.alert-box {
  display: flex;
  align-items: center;
  background: #13ce66;
  padding: 10px 0 10px 10px;
  border-radius: 4px;
}
</style>